﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class RezidentEditForm : Form
    {
        private string mode;

        private Form parentForm;

        public Rezident rezident;

        private bool act = false;

        public RezidentEditForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Конструктор на добавление
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        public RezidentEditForm(Form parentForm, string mode)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Резидент [{0}]", mode);

            this.parentForm = parentForm;

            if (this.mode == "Добавление")
            {
                rezident = new Rezident();
                act = true;
            }
        }

        /// <summary>
        /// Конструктор на изменение
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        /// <param name="id"></param>
        public RezidentEditForm(Form parentForm, string mode, int id)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Резидент [{0}]", mode);

            this.parentForm = parentForm;

            if (mode == "Изменение")
            {
                rezident = new Rezident();

                Tools.RunWithProgressIndicator(this, delegate
                {
                    rezident.SelectById(id);

                    tbxName.Text = rezident.Name;
                    tbxName2.Text = rezident.Name2;  
                    tbxDetails.Text = rezident.Details;
                    tbxNote.Text = rezident.Note;

                    tbxINN.Text = rezident.INN;
                    tbxKPP.Text = rezident.KPP;
                    tbxOGRN.Text = rezident.OGRN;
                    tbxNAccount.Text = rezident.n_account;
                    tbxKorAccount.Text = rezident.kor_account;
                    tbxBankName.Text = rezident.bank_name;
                    tbxBIK.Text = rezident.BIK;
                    tbxDirector.Text = rezident.director;
                    tbxActingOnTheBasis.Text = rezident.acting_of_the_basis;
                    tbxUAddress.Text = rezident.ur_address;
                    tbxPostAddress.Text = rezident.post_address;
                    tbxTelNumber.Text = rezident.tel_number;
                    tbxFaxNumber.Text = rezident.fax_number;
                    tbxEmail.Text = rezident.email;

                });
            }

            this.act = true;
        }

        ///// <summary>
        ///// НЕ ПОМНЮ ЗАЧЕМ ЭТОТ МЕТОД
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void rebuildRezidentData(object sender, EventArgs e)
        //{
        //    if (act)
        //    {
        //        this.rezident.Name = tbxName.Text.Trim();
        //        this.rezident.Name2 = tbxName2.Text.Trim();
        //        this.rezident.Details = tbxDetails.Text.Trim();
        //        this.rezident.Note = tbxNote.Text.Trim();
        //    }
        //}

        private bool IsFormValid()
        {
            if (tbxName.Text.Length == 0 ||
                 tbxName2.Text.Length == 0)
            {
                MessageBox.Show("Не все обязательные для заполнения поля заполнены", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bSuccessfully = false;

            try
            {
                if (!IsFormValid())
                {
                    return;
                }

                this.rezident.Name = tbxName.Text.Trim();
                this.rezident.Name2 = tbxName2.Text.Trim();
                this.rezident.Details = tbxDetails.Text.Trim();
                this.rezident.Note = tbxNote.Text.Trim();
                
                if(rbtnIP.Checked)
                {
                    this.rezident.Type="ИП";
                }
                else
                {
                    this.rezident.Type="ЮЛ";
                }

                this.rezident.INN = tbxINN.Text.Trim();
                this.rezident.KPP = tbxKPP.Text.Trim();
                this.rezident.OGRN = tbxOGRN.Text.Trim();
                this.rezident.n_account = tbxNAccount.Text.Trim();
                this.rezident.kor_account = tbxKorAccount.Text.Trim();
                this.rezident.bank_name = tbxBankName.Text.Trim();
                this.rezident.BIK = tbxBIK.Text.Trim();
                this.rezident.director = tbxDirector.Text.Trim();
                this.rezident.acting_of_the_basis = tbxActingOnTheBasis.Text.Trim();
                this.rezident.ur_address = tbxUAddress.Text.Trim();
                this.rezident.post_address = tbxPostAddress.Text.Trim();
                this.rezident.tel_number = tbxTelNumber.Text.Trim();
                this.rezident.fax_number = tbxFaxNumber.Text.Trim();
                this.rezident.email  = tbxEmail.Text.Trim();

                if (this.mode == "Изменение")
                {
                    Tools.RunWithProgressIndicator(this, delegate { rezident.Update(); });

                    MessageBox.Show("Резидент изменен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    if (mode == "Добавление")
                    {
                        Tools.RunWithProgressIndicator(this, delegate { rezident.Insert(); });

                        MessageBox.Show("Резидент добавлен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                //if (parentForm is RezidentForm)
                //{
                //    (parentForm as RezidentForm).UpdateForm();
                //}

                bSuccessfully = true;
            }
            catch
            {
                ;
            }

            if (bSuccessfully)
            {
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// Проверка ИП/ЮЛ
        /// </summary>
        private void CheckType(object sender, EventArgs e)
        {
            

            if (rbtnIP.Checked)
            {
                label8.Enabled = false;
                tbxKPP.Enabled = false;
                label9.Text = "ОГРНИП";
                tbxDirector.Enabled = false;
            }
            else
            {
                if (rbtnUL.Checked)
                {
                    label8.Enabled = true;
                    tbxKPP.Enabled = true;
                    label9.Text = "ОГРН";
                    tbxDirector.Enabled = true;
                }
            }
        }

        private void CheckType()
        {

        }

        private void RezidentEditForm_Load(object sender, EventArgs e)
        {
            if (this.rezident.Type == "ИП")
            {
                rbtnIP.Checked = true;
            }
            else
            {
                rbtnUL.Checked = true;
            }

            CheckType(this, e);
        }
    }
}
